define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index',
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");
            var brand_list = table.attr('brand-list');
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                queryParams: function (params) {
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    filter.status = 1;
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'cate_id', title: __('Cate_title'), visible: false, searchList: $.getJSON('goods/cateList')},
                        {field: 'brand_id', title: __('Brand_title'), visible: false, searchList: $.getJSON('goods/brandList')},
                        {
                            field: 'goods_info',
                            title: __('Goods_title'),
                            formatter: Controller.api.formatter.goods_info,
                            operate: false,
                            align: 'left',
                            width: '20%'
                        },
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: 'BETWEEN'},
                        {field: 'market_price', title: __('Market_price'), operate: false},
                        {field: 'selling_price', title: __('Selling_price'), operate: 'BETWEEN'},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    if (typeStr != 'all') {
                        //如果希望追加搜索条件,可使用
                        var filter = params.filter ? JSON.parse(params.filter) : {};
                        var op = params.op ? JSON.parse(params.op) : {};
                        filter.status = typeStr;
                        params.filter = JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                    }

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $(table).on("click", "[data-id].btn-down", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                Layer.confirm(
                        __('确定下架商品吗？'),
                        {icon: 3, title: __('确定下架商品吗？'), shadeClose: true},
                        function (index) {
                            $.post('goods/down', {goods_id: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });
            $(table).on("click", "[data-id].btn-up", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                Layer.confirm(
                        __('确定上架商品吗？'),
                        {icon: 3, title: __('确定上架商品吗？'), shadeClose: true},
                        function (index) {
                            $.post('goods/up', {goods_id: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            $(table).on("click", "[data-id].btn-delgoods", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                Layer.confirm(
                        __('确定删除商品吗？'),
                        {icon: 3, title: __('确定删除商品吗？'), shadeClose: true},
                        function (index) {
                            $.post('goods/delgoods', {goods_id: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                goods_info: function (value, row, index) {
                    var html = '<div style="width:250px;"><img class="img-sm img-center" src="' + Fast.api.cdnurl(row.goods_logo) + '">&nbsp;';
                    html += row.goods_title + ',' + row.goods_model + ',' + row.goods_spec + '</div>';
                    return html;
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
//                    html += '<a href="user/detail/ids/127" class="btn-change text-success" style="margin-left: 10px;" title="用户信息" data-table-id="table" data-field-index="11" data-row-index="0" data-button-index="2"><b>用户信息</b></a>';

                    var html = '<a href="goods/edit/ids/' + row.goods_id + '" class="btn-dialog" title="编辑" data-table-id="table"><b>编辑</b></a> |';
                    if (row.status == 1) {
                        html += '<a class="text-success btn-down"  style="cursor:pointer" data-url="goods/up" data-id="' + row.goods_id + '"><b>下架</b></a>';

                    } else {
                        html += '<a class="text-success btn-up"  style="cursor:pointer" data-url="goods/down" data-id="' + row.goods_id + '"><b>上架</b></a>|';
                        html += '<a class="text-danger btn-delgoods"  style="cursor:pointer" data-url="goods/delgoods" data-id="' + row.goods_id + '"><b>删除</b></a>';
                    }

                    return html;
                }
            },
        }
    };
    return Controller;
});